// pages/jsdd/jsdd.js
import instance from "../../utils/util"
import {
    formatTime
} from "../../utils/format"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        address: {},
        totalPrice: 0,
        buyName: "",
        show: false,
        deliveryDate: "",
        buyPhone: "",
        remarks: "",
        minDate: new Date().getTime(),

        currentDate: new Date().getTime(),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (!options.id) {
            this.getinfo()

        } else {
            this.getingo(options.id)
        }

    },
    onInput(e) {
        console.log(e)
    },
    onConfirmTimerPicker(e) {
        console.log(e)
        this.setData({
            deliveryDate: formatTime(new Date(e.detail)),
            show: false
        })
    },
    oncacel(e) {
        this.setData({
            show: false
        })
    },
    async onSubmit() {
        if (this.data.buyName == "") {
            wx.showToast({
                title: '请输入订购人姓名',

                icon: "none"
            })
            return
        }
        if (this.data.buyPhone == "") {
            wx.showToast({
                title: '请输入订购人电话',

                icon: "none"
            })
            return
        }
        if (this.data.deliveryDate == "") {
            wx.showToast({
                title: '请选择日期',
                icon: "none"
            })
            return
        }
        const res = await instance.post("/order/submitOrder", {
            buyName: this.data.buyName,
            buyPhone: this.data.buyPhone,
            cartList: this.data.cart,
            deliveryDate: this.data.deliveryDate,
            remarks: this.data.remarks,
            userAddressId: this.data.address.id
        })
        console.log(res);
        this.getxp(res.data)

    },
    async getxp(e) {
        const res = await instance.get(`/webChat/createJsapi/${e}`);
        console.log(res)
        const ress =await wx.requestPayment(res.data);
        console.log(ress);
        if(ress.errMsg === 'requestPayment:ok'){
            const res1=await instance.get(`/webChat/queryPayStatus/${e}`);
            console.log(res1);
            if(res1.code == 200){
                wx.showToast({
                  title: '支付成功',
                })
                wx.navigateTo({
                  url: '/pages/order/order',
                })
            }
        }

    },
    async getingo(e) {
        const res = await instance.get(`/order/buy/${e}?blessing=''`, );
        console.log(res)
        this.setData({
            cart: res.data.cartVoList,
            totalPrice: res.data.totalAmount
        })
    },
    addresslog() {
        wx.navigateTo({
            url: '/pages/setaddress/setaddress',
        })
    },
    async getaddress() {
        const res = await instance.get("/userAddress/getOrderAddress");
        console.log(res)
        this.setData({
            address: res.data
        })
    },
    async getinfo() {
        const res = await instance.get("/order/trade");
        console.log(res)
        this.setData({
            cart: res.data.cartVoList,
            totalPrice: res.data.totalAmount
        })
    },
    showdate() {
        this.setData({
            show: true
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getaddress()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})